var express = require('express');
var router = express.Router();
let {listModel,cateModel}=require('../model/model')
/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });

router.get('/cate',async(req,res)=>{
    let data=await cateModel.find()
    res.send({
      code:200,
      msg:"展示成功",
      data
    })
})

router.post('/cateadd',async(req,res)=>{
  await cateModel.create(req.body)
  res.send({
    code:200,
    msg:"添加成功",
  })
})


router.get('/list',async(req,res)=>{
  let data=await listModel.find()
  res.send({
    code:200,
    msg:"展示成功",
    data
  })
})

router.post('/listadd',async(req,res)=>{
  await listModel.create(req.body)
  res.send({
    code:200,
    msg:"添加成功",
  })
})

router.put('/addnum/:id',async(req,res)=>{
  let id=req.params.id
  await listModel.updateOne({ _id : id },{ $inc:{num : 1} })
  res.send({
    code:200,
    msg:"修改成功",
  })
})

router.put('/subnum/:id',async(req,res)=>{
  let id=req.params.id
  await listModel.updateOne({ _id : id },{ $inc:{num : -1} })
  res.send({
    code:200,
    msg:"修改成功",
  })
})


router.put('/setnum',async(req,res)=>{
  await listModel.updateMany({},{ $set:{num : 0} })
  res.send({
    code: 200,
    msg: "ok"
  })
})
module.exports = router;
